<?php
// +----------------------------------------------------------------------
// |     【FabricColor】Controller 控制器
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\admin\controller\fabric;

use think\facade\Request;
use app\common\service\FabricColor as S;
use app\common\model\FabricColor as M;
use app\common\model\FabricMain as F_M;

class Color extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];

	//初始化
	public function initialize(){
		parent::initialize();
		
		$this->assign('controller_title','面料颜色');
	}

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList());
        }
        return $this->fetch();
    }
    
    // 颜色表格
    public function table(){
        $pid=I('fabric_id',0);
        if (Request::isAjax()) {
            return $this->getJson(M::getList());
        }
        return $this->fetch('',[
            'pid'=>$pid
        ]);
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
            return $this->getJson(S::goAdd(Request::post()));
        }
        
        $id=I('fabric_id',0);
        $model = [];
        if($id>0){
            $fabric=F_M::info($id);
            $model=[
                'fabric_id'=>$fabric['id'],
                'fabric_title'=>$fabric['fabric_title'],
                'sup_id'=>$fabric['sup_id'],
                'sup_name'=>$fabric['sup_name'],
                'sup_code'=>$fabric['sup_code'],
            ];
        }
        
        return $this->fetch('edit',[
			'action_up'=>false,
            'options'=>config_cache_file('options'),
            'model' => $model,
		]);
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(),$id));
        }
		
		$model=M::info(['id'=>$id]);
		if(!$model) $this->getJson(102);
        
        $id=I('fabric_id',0);
        if($id>0){
            $fabric=F_M::info($id);
            $model['fabric_id']=$fabric['id'];
            $model['fabric_title']=$fabric['fabric_title'];
            $model['sup_id']=$fabric['sup_id'];
            $model['sup_name']=$fabric['sup_name'];
            $model['channel_code']=$fabric['sup_code'];    
        }
        
		return $this->fetch('',[
			'action_up'=>true,
            'options'=>config_cache_file('options'),
			'model' => $model,
		]);
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post(),$id));
    }

    // 删除
    public function remove($id){
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove(){
        return $this->getJson(S::goBatchRemove([
				['id','in',Request::post('ids')],
			]));
    }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle());
        }
        return $this->fetch();
    }
	
    // 设置某个值
    public function setValue(){
        if (Request::isAjax()) {
            return $this->getJson(S::setFieldValue(Request::post()));
        }
        return $this->fetch();
    }
}
